package com.zoo.diannaopeijian.controller;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.domain.Repertory;
import com.zoo.diannaopeijian.service.RepertoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 仓库控制器
 */
@Controller
@RequestMapping(value = "/repertory")
public class RepertoryController extends BaseController {
    @Resource
    private RepertoryService repertoryService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "repertory/" + pageName;
    }

//    @GetMapping(value = "warehouseManaPage")
//    public String warehouseManaPageRoute() {
//        return "warehouse/warehouseManaPage";
//    }
//
//    @GetMapping(value = "warehouseAddPage")
//    public String warehouseAddPageRoute() {
//        return "warehouse/warehouseAddPage";
//    }
//
//    @GetMapping(value = "warehouseUpdatePage")
//    public String warehouseUpdatePageRoute() {
//        return "warehouse/warehouseUpdatePage";
//    }

    @GetMapping(value = "list")
    public ResponseEntity<Map<String, Object>> repertoryListGet(
            Page page,
            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
            Repertory repertory
    ) {
        return ResponseEntity.ok(buildPageResult(isFuzzy ? repertoryService.getFuzzyRepertoryWithPage(page, repertory) : repertoryService.getRepertoryWithPage(page, repertory)));
    }

    @GetMapping(value = "/")
    public ResponseEntity<List<Repertory>> repertoryGet() {
        return ResponseEntity.ok(repertoryService.getRepertory(null));
    }

    @PostMapping(value = "/")
    public ResponseEntity<String> repertoryPost(Repertory repertory) {
        return ResponseEntity.ok(repertoryService.addRepertory(repertory) ? success : fail);
    }

    @DeleteMapping(value = "/{repertoryId}")
    public ResponseEntity<String> repertoryDelete(@PathVariable Integer repertoryId) {
        return ResponseEntity.ok(repertoryService.deleteRepertoryById(repertoryId) ? success : fail);
    }

    @PutMapping(value = "/")
    public ResponseEntity<String> repertoryUpdate(Repertory repertory) {
        return ResponseEntity.ok(repertoryService.updateRepertory(repertory) ? success : fail);
    }
}
